﻿using Cofe.Core.Interfaces;
using Cofe.Core.Logger;
using Cofe.Core.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace Cofe.Core.Implements
{
//#if NETFX_CORE
//    [DataContract]
//#else
//[Serializable]
//#endif    
    public class VolumeInfo : IVolumeInfo, IXmlSerializable
    {        
        public string FactoryId { get; set; }    
        public string VolumeId { get; set; }        
        public string ParamString { get; set; }

        public static VolumeInfo None = new VolumeInfo() { FactoryId = "", VolumeId = "", ParamString = "" };

        public VolumeInfo()
        {

        }

        public VolumeInfo(string factoryId, string volumeId, string paramString)
        {
            FactoryId = factoryId;
            VolumeId = volumeId;
            ParamString = paramString;
        }

        public override bool Equals(object obj)
        {
            if (obj is IVolumeInfo)
            {
                IVolumeInfo objVolume = obj as IVolumeInfo;
                return FactoryId == objVolume.FactoryId &&
                    VolumeId == objVolume.VolumeId &&
                    ParamString == objVolume.ParamString;
            }
            return false;
        }

        public override int GetHashCode()
        {
            return (FactoryId + VolumeId + ParamString).GetHashCode();
        }

        public System.Xml.Schema.XmlSchema GetSchema()
        {
            throw new NotImplementedException();
        }

        public void ReadXml(System.Xml.XmlReader reader)
        {
            reader.MoveToContent();
            FactoryId = reader.GetAttribute("FactoryId");
            VolumeId = reader.GetAttribute("VolumeId");
            ParamString = reader.GetAttribute("ParamString");
        }

        public void WriteXml(System.Xml.XmlWriter writer)
        {
            writer.WriteAttributeString("FactoryId", FactoryId);
            writer.WriteAttributeString("VolumeId", VolumeId);
            writer.WriteAttributeString("ParamString", ParamString);
        }
    }

    public class Volume : VolumeInfo, IVolume
    {
        #region Constructor

        public Volume(string factoryId, string volumeId, string paramString,
            Dictionary<string, string> paramDic, IDirectoryLister directoryLister, params IEventSource[] eventSources)
        {
            FactoryId = factoryId;
            VolumeId = volumeId;
            ParamString = paramString;
            Parameters = paramDic;
            directoryLister.Volume = this;
            DirectoryLister = directoryLister;
            EventSources = eventSources;
        }

        public Volume(string factoryId, string volumeId, string paramString,
            IParamParser paramParser, IDirectoryLister directoryLister, params IEventSource[] eventSources)
            : this(factoryId, volumeId, paramString, paramParser.StringToDictionary(paramString), directoryLister, eventSources)
        {

        }

        public Volume(IVolumeInfo volumeInfo,
          IParamParser paramParser, IDirectoryLister directoryLister, params IEventSource[] eventSources)
            : this(volumeInfo.FactoryId, volumeInfo.VolumeId, volumeInfo.ParamString, 
            paramParser.StringToDictionary(volumeInfo.ParamString), directoryLister, eventSources)
        {

        }  

        #endregion
        #region Methods

        public void Dispose()
        {
            foreach (var ec in EventSources)
                ec.Dispose();
        }

        #endregion

        #region Data

        #endregion

        #region Public Properties

        public Dictionary<string, string> Parameters { get; private set; }
        public IDirectoryLister DirectoryLister { get; private set; }
        public IEventSource[] EventSources { get; private set; }
    
        #endregion

       
    }
}
